package co.laomag.coffee.controller.amins;

import cn.hutool.core.date.DateUtil;
import co.laomag.coffee.api.admins.AdminApi;
import co.laomag.coffee.model.entity.Admin;
import co.laomag.coffee.model.vo.Login;
import co.laomag.coffee.model.vo.Message;
import co.laomag.coffee.service.admins.AdminService;
import co.laomag.coffee.utils.ResultUtils;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;


/**
 * @author 马泽朋
 * @version 1.0
 * @date 2020/4/18 下午 3:00
 */
@RestController
public class AdminController implements AdminApi {
    @Resource
    private AdminService adminService;

    @Override
    public Message administratorLogin(@RequestBody Login login) {
        Message message = adminService.administratorLogin(login.getUsername(), login.getPassword());
        return message;
    }

    @Override
    public Message addAdministrator(@RequestBody Admin admin) {
        admin.setCreatetime(DateUtil.date());
        admin.setUpdatetime(DateUtil.date());
        Message message = null;
        try {
            message = adminService.addAdministrator(admin);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtils.message(200, "新增失败,请稍后再试");
        }
        return message;
    }

    @Override
    public Message getRouters(String token) {
        return adminService.getRouters(token);
    }

    @Override
    public Message update(@RequestBody Admin admin) {
        Message update = null;
        try {
            update = adminService.update(admin);
        } catch (Exception e) {
            return ResultUtils.message(5001, "修改失败,请稍后再试");
        }
        return update;
    }

    @Override
    public Message getRoles() {
        return adminService.getRoles();
    }

    @Override
    public Message getAdminList(String title, int page, int limit,@RequestHeader("X-Token") String token) {
        return adminService.getAdminList(title, page, limit,token);
    }

    @Override
    public Message deleteAdmin(@RequestBody Admin admin) {
        Message message = null;
        try {
            message = adminService.deleteAdmin(admin);
        } catch (Exception e) {
            return ResultUtils.message(5001, "删除失败,请稍后再试");
        }
        return message;
    }
}
